package com.example.studentsystem.db;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.studentsystem.Bean.Student;
import com.example.studentsystem.R;

public class ChangePassWord extends AppCompatActivity {
    private Button mChangeBackSet;
    private Button mbuttonModifyPassWord;
    private EditText medit2;
    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.change_password);
        mChangeBackSet=findViewById(R.id.ChangeBackSet);
        mbuttonModifyPassWord=findViewById(R.id.buttonModifyPassWord);
        medit2=findViewById(R.id.edit2);
        mChangeBackSet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mChangeBackSet.setVisibility(View.VISIBLE);
                Intent intent=new Intent(ChangePassWord.this,Setting.class);
                startActivity(intent);
            }
        });
        mbuttonModifyPassWord.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String changedpassword = medit2.getText().toString();
                if (v.getId() == R.id.buttonModifyPassWord){
                    if (allInputNotEmpty(changedpassword)) {
                        Student student = new Student();
                        student.setPassword(changedpassword);
                        int id = (int) DBHelper.getInstance(ChangePassWord.this).UpdatePassWord(student);
                        if (id >= 0) {
                            Toast.makeText(ChangePassWord.this, "修改成功", Toast.LENGTH_SHORT).show();
                            medit2.setText("");
                        }else {
                            Toast.makeText(ChangePassWord.this, "修改失败", Toast.LENGTH_SHORT).show();
                        }
                    }
                }
            }
            private boolean allInputNotEmpty(String... inputStrings) {
                for (int i = 0; i < inputStrings.length; i++) {
                    if (TextUtils.isEmpty(inputStrings[i])) {
                        return false;
                    }
                }
                return true;
            }
        });

    }
}
